package com.zmn.plat.admin.controller.service.tariff;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.business.interfaces.service.tariff.ServiceTariffBService;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;
import com.zmn.plat.model.vo.service.tariff.ServiceTariffVO;

/**
 * 故障价格名称表，统一名称管理用
 *
 * @author tanbiao
 * @since 2019-11-21 18:56:50
 */
@RestController
@RequestMapping("/service/tariff/")
public class ServiceTariffModifyController extends ServiceTariffBaseController {
    @Resource
    protected ServiceTariffBService faultTariffBService;

    /**
     * 新增修改
     *
     * @param faultTariff
     * @author tanbiao
     * @since 2019-11-21 18:56:50
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/service/tariff/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ServiceTariff faultTariff) {
        Integer tariffId = faultTariff.getTariffId();
        faultTariff.setFirstLetter(InitialUtil.getFristEnName(faultTariff.getTariffName()));
        // 限制条件：1同类型价格表名称不可重复
        // 3.停用价格表，则其关联的服务分类+后台一级产品分类的价格表，以及：产品关联的价格表，也都将禁用
        try {
            if (tariffId == null || tariffId == 0) {
                faultTariffBService.insertAll(faultTariff, getMcStaffRealName());
            } else {
                faultTariffBService.updateAll(faultTariff, getMcStaffRealName());
            }
            return ResponseDTO.success(Integer.toString(faultTariff.getTariffId()), "操作成功！");
        } catch (PlatException ex) {
            logger.error("数据异常！", faultTariff.getTariffName());
            return ResponseDTO.fail(ex.getMessage());
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("不能重复添加！", faultTariff.getTariffName());
            return ResponseDTO.fail("数据已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改故障价格名称表，统一名称管理用失败", ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     * @author tanbiao
     * @since 2019-11-21 18:56:50
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/service/tariff/list.action")
    public ResponseDTO updateSort(HttpServletRequest request, @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }

        Integer result = faultTariffService.updateSortByKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @author tanbiao
     * @since 2019-11-21 18:56:50
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/service/tariff/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (status == 0) {
            return ResponseDTO.fail("请设置状态！");
        }

        // 1.停用价格表，则其关联的服务分类+后台一级产品分类的价格表，以及：产品关联的价格表，也都将禁用
        // 2、默认价格表不可被停用
        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        try {
            Integer result = faultTariffBService.updateStatusBatchByKey(arrayIds, status, getMcStaffRealName());
            return ResponseDTO.success(result, "操作成功！");
        } catch (PlatException ex) {
            logger.error("数据异常！", ids);
            return ResponseDTO.fail(ex.getMessage());
        } catch (Exception ex) {
            logger.error("修改状态异常,内部错误", ex);
            return ResponseDTO.fail("操作失败！");
        }

    }

    /**
     * 描述:修改首字母
     *
     * @param tariffId
     *            主键id
     * @param firstLetter
     *            首字母
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:20191022
     */
    @RequestMapping(value = "updateFirstLetter")
    @RequiresPermissions("/service/tariff/list.action")
    public ResponseDTO updateFirstLetter(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer tariffId,
        @RequestParam(value = "firstLetter", defaultValue = "") String firstLetter) {

        if (tariffId == 0) {
            return ResponseDTO.fail("请选择要更改首字母的表名称！");
        }
        if (StringUtil.isBlank(firstLetter)) {
            return ResponseDTO.fail("请设置分类首字母！");
        }
        Integer result = faultTariffService.updateFirstLetterByKey(tariffId, firstLetter, getMcStaffRealName());
        return ResponseDTO.success(result, "操作成功！");
    }

    /**
     * 描述:复制价格表
     *
     * @param faultTariffVO
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since:20200305
     */
    @RequestMapping(value = "copyTariff")
    @RequiresPermissions("/service/tariff/copy.action")
    public ResponseDTO copyTariff(HttpServletRequest request, @RequestBody ServiceTariffVO faultTariffVO) {

        if (faultTariffVO.getOrganTariffId() == null || faultTariffVO.getNewTariffId() == null) {
            return ResponseDTO.fail("请选择价格表！");
        }
        if (faultTariffVO.getServCategId() == null) {
            return ResponseDTO.fail("请选择分类！");
        }
        try {
            faultTariffBService.copyTariff(faultTariffVO, getMcStaffRealName());
            return ResponseDTO.success(GlobalConsts.YES, "复制成功！");
        } catch (PlatException ex) {
            logger.error("复制价格表数据异常！", ex);
            return ResponseDTO.fail(ex.getMessage());
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("复制价格表不能重复添加！", ex);
            return ResponseDTO.fail("原价格表中存在已经复制但未改名的分组，请修改之后再执行");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改故障价格名称表，统一名称管理用失败", ex);
            return ResponseDTO.fail("操作失败！");
        }

    }

}
